Welcome![Sign In][Sign Up]
Location:
Search - V c

Search list

[Other用c编写的N*N的螺旋矩阵源代码

Description:

/*
实现效果:
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
*/
#include <stdio.h>
#define N 5 //阶数,即N*N的螺旋矩阵

void main()
{
    int i, j, num=1, a[N][N];
    for(i=0; i<=N/2; i++)
    {
        for(j=i; j<N-i; j++) a[i][j]=n++;
        for(j=i+1; j<N-i; j++) a[j][N-i-1]=n++;
        for(j=N-i-2; j>i; j--) a[N-i-1][j]=n++;
        for(j=N-i-1; j>i; j--) a[j][i]=n++;
    }
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
            printf("%2d ",a[i][j]);
        printf("\n");
    }
}
    

 

不知道叫什么,先叫它“回宫图”吧
年初的时候在贴吧瞎逛,看到了一个程序挺有意思,会输出如下的形状:
01 24 23 22 21 20 19
02 25 40 39 38 37 18
03 26 41 48 47 36 17
04 27 42 49 46 35 16
05 28 43 44 45 34 15
06 29 30 31 32 33 14
07 08 09 10 11 12 13
仔细看这个形状,数字是按顺序往里回旋的,觉得很有创意,可是一看源代码头就大了,
每个编程人都知道看别人的代码是很困难的,尤其像这种不知道思路的,所以也就放下
没管了。
昨天上物理课实在是没心思听,就想起这个程序,想了一节课,果然不负有心人,给弄出来了,这个是增强版的,可以输入1-10中的任意个数,然后生成图形。
先看代码,没有注释,所以不好看的懂。
#include<stdio.h>
main()
{
       int n,m,i,j,t,k=1;
       int a[11][11];
       clrscr();
       do{
       printf("please input a number(1-10):");
       scanf("%d",&n);
       }while(n<1||n>10);
       t=n+1;
       for(m=1;m<=t/2;m++)
         {
           for(i=m;i<=t-m;i++)
             {a[i][m]=k;k++;}
           for(j=m+1;j<=t-m;j++)
             {a[i-1][j]=k;k++;}
           for(i=n-m;i>=m;i--)
             {a[i][j-1]=k;k++;}
           for(j=n-m;j>=m+1;j--)
             {a[i+1][j]=k;k++;}
         }
       for(i=1;i<=n;i++)
         {
           for(j=1;j<=n;j++)
             {
               if(a[i][j]<=9) printf("0%d ",a[i][j]);
               else printf("%d ",a[i][j]);       }
           printf("\n");
         }
       getch();
}
就是这样的。


可以更简洁些:

#include<stdio.h>
main()
{
       int n,m,i,j,t,k=1;
       int a[11][11];
       clrscr();
       do{
       printf("please input a number(1-10):");
       scanf("%d",&n);
       }while(n<1||n>10);
       t=n+1;
       for(m=1;m<=t/2;m++)
         {
           for(i=m;i<=t-m;i++)
             a[i][m]=k++;
           for(j=m+1;j<=t-m;j++)
             a[i-1][j]=k++;
           for(i=n-m;i>=m;i--)
             a[i][j-1]=k++;
           for(j=n-m;j>=m+1;j--)
             a[i+1][j]=k++;
         }
       for(i=1;i<=n;i++)
         {
           for(j=1;j<=n;j++)
             {
               if(a[i][j]<=9) printf("0%d ",a[i][j]);
               else printf("%d ",a[i][j]);       }
           printf("\n");
         }
       getch();
}

 


 #include <stdio.h>
#define N 8
main(){
 int i,j,n=1,a[N][N];
 for(i=0;i<=N/2;i++){
  for(j=i;j<N-i;j++)
   a[i][j]=n++;
  for(j=i+1;j<N-i;j++)
   a[j][N-i-1]=n++;
  for(j=N-i-2;j>i;j--)
   a[N-i-1][j]=n++;
  for(j=N-i-1;j>i;j--)
   a[j][i]=n++;
 }
 for(i=0;i<N;i++){
  printf("\n\n");
  for(j=0;j<N;j++)
   printf("%5d",a[i][j]);
 }
}
 

 


                                马踏棋盘问题


#include <stdio.h>
#define N 5
void main(){
 int x,y;
 void horse(int i,int j);
 printf("Please input start position:");
 scanf("%d%d",&x,&y);
 horse(x-1,y-1);
}
void horse(int i,int j){
 int a[N][N]={0},start=0,
  h[]={1,2,2,1,-1,-2,-2,-1},
  v[]={2,1,-1,-2,2,1,-1,-2},
  save[N*N]={0},posnum=0,ti,tj,count=0;
 int jump(int i,int j,int a[N][N]);
 void outplan(int a[N][N]);
 a[i][j]=posnum+1;
 while(posnum>=0){
  ti=i;tj=j;
  for(start=save[posnum];start<8;++start){
   ti+=h[start];tj+=v[start];
   if(jump(ti,tj,a))
    break;
   ti-=h[start];tj-=v[start];
  }
  if(start<8){
   save[posnum]=start;
   a[ti][tj]=++posnum+1;
   i=ti;j=tj;save[posnum]=0;
   if(posnum==N*N-1){
    //outplan(a);
    count++;
   }
  }
  else{
   a[i][j]=0;
   posnum--;
   i-=h[save[posnum>;j-=v[save[posnum>;
   save[posnum]++;
  }
 }
 printf("%5d",count);
}
int jump(int i,int j,int a[N][N]){
 if(i<N&&i>=0&&j<N&&j>=0&&a[i][j]==0)
  return 1;
 return 0;
}
void outplan(int a[N][N]){
 int i,j;
 for(i=0;i<N;i++){
  for(j=0;j<N;j++)
   printf("%3d",a[i][j]);
  printf("\n");
 }
 printf("\n");
 //getchar();
}
用回溯法得到所有的解,但效率较低,只能算出5行5列的

 


Platform: | Size: 4395 | Author: good@588 | Hits:

[Other resourcepkuVisualC

Description: 北大BBS VC++版精华下栽,内有全部c库函数- Beijing University BBS under V C + + version essence plants, in has completely the c storehouse function
Platform: | Size: 15357512 | Author: 邓海峰 | Hits:

[Speech/Voice recognition/combine指纹识别的程序(v c++)

Description: 指纹识别的程序(c++)-Fingerprint Recognition Procedure (c)
Platform: | Size: 387507 | Author: 李翔 | Hits:

[Chess Poker gamesv欢乐五子棋C#版(带源码)

Description: v欢乐五子棋C#版(带源码).rar-v joy C# 331 version (with source code). Rar
Platform: | Size: 64512 | Author: 陈利民 | Hits:

[DocumentsC键盘画图

Description: 用C语言实现键盘画图,"V”:画笔提起 "W”:开始画图 "R”:开始擦图 "S”:当前图形存入文件 "E”:调出已有文件 "C”:画圆-C language keyboard drawing, the "V" : brush name "W" : start drawing "R" : start rubbing map "S" : The current graphics into documents "E" : the redeployment of existing files "C" : Circle
Platform: | Size: 2048 | Author: 宋瑞轩 | Hits:

[VC/MFCVisual C++_NET技术内幕 (_11039236

Description: Vc++.net技术内幕,内容介绍的比较全面,而且通俗易懂-inside visual v++
Platform: | Size: 23143424 | Author: 和平鸽 | Hits:

[SCMμC/OS—Ⅱ FOR ATMega256

Description: μC/OS—ⅡV:2.80 对 ATMega256 单片机的移植程序代码。- C/OS- II V : 2.80 pairs ATMega256 SCM transplant procedure code.
Platform: | Size: 27648 | Author: 磊元 | Hits:

[Speech/Voice recognition/combine指纹识别的程序(v c++)

Description: 指纹识别的程序(c++)-Fingerprint Recognition Procedure (c)
Platform: | Size: 387072 | Author: 李翔 | Hits:

[GUI Develop时钟小程序 v 1.0

Description: 时钟小程序 v 1.0功能: 显示当前时间 界面有半透明效果 能锁定到桌面,使之不能移动 当然也能解锁,使之能够移动到你想要的地方 结束程序,退出 -clock small programs v 1.0 features : they show the current time interface with translucent effects can be locked to a desktop, so of course they are not mobile can be unlocked so that it can moved to the place you want to end the process, from
Platform: | Size: 64512 | Author: 胡建伟 | Hits:

[Anti-virusa-v

Description: 用C编写的简单的杀毒程序,主要利用了一般的杀毒原理实现.-prepared by the simple anti-virus procedures, primarily the use of the general principles of achieving toxicity.
Platform: | Size: 4096 | Author: 装敏 | Hits:

[OS Developsendokyeswhywhatwhere

Description: 用Evc开发的一些关于文件操作的程序实例源码,供大家下载参考学习- With E v c development some about the document operation procedure example source code, study for everybody downloading reference -Evc development with the document on the operation of the program is the source code, Download reference for all learning-E v c With some about the development document operation procedure example source c ode. study for everybody downloading reference
Platform: | Size: 229376 | Author: steven | Hits:

[SCMc

Description: 单片机C语言学习好资料 前言 2 基础知识:单片机编程基础 2 第一节:单数码管按键显示 4 第二节:双数码管可调秒表 6 第三节:十字路口交通灯 6 第四节:数码管驱动 7 第五节:键盘驱动 8 第六节:低频频率计 14 第七节:电子表 17 第八节:串行口应用 17 -Singlechip C language learning good basic knowledge of information Preface 2: Single-chip programming based on 2 Section I: Single-button digital tube display 4 Section II: dual digital tube 6 adjustable stopwatch III: traffic lights at a crossroads 6 4: digital tube drive 7 Section V: 8 of section VI of the keyboard driver: low-frequency frequency meter 14, Section VII: Electronic Table 17 in section VIII: Application of the serial port 17
Platform: | Size: 53248 | Author: 华羿 | Hits:

[OtherC-yuyan100li

Description: 文中讲诉了有关c语言常见10例程的介绍 次例程都是很好的啊-The paper stresses v. c language common on the 10 routines routine introduction times are good ah
Platform: | Size: 35840 | Author: 于华 | Hits:

[source in ebookFloyd-Warshall-c-chengxi

Description: Floyd-Warshall算法描述 1)适用范围: a)APSP(All Pairs Shortest Paths) b)稠密图效果最佳 c)边权可正可负 2)算法描述: a)初始化:dis[u,v]=w[u,v] b)For k:=1 to n For i:=1 to n For j:=1 to n If dis[i,j]>dis[i,k]+dis[k,j] Then Dis[I,j]:=dis[I,k]+dis[k,j] c)算法结束:dis即为所有点对的最短路径矩阵 3)算法小结:此算法简单有效,由于三重循环结构紧凑,对于稠密图,效率要高于执行|V|次Dijkstra算法。时间复杂度O(n^3)。 考虑下列变形:如(I,j)∈E则dis[I,j]初始为1,else初始为0,这样的Floyd算法最后的最短路径矩阵即成为一个判断I,j是否有通路的矩阵。更简单的,我们可以把dis设成boolean类型,则每次可以用“dis[I,j]:=dis[I,j]or(dis[I,k]and dis[k,j])”来代替算法描述中的蓝色部分,可以更直观地得到I,j的连通情况。 -err
Platform: | Size: 3072 | Author: 江晨 | Hits:

[Menu control007

Description: 任务栏托盘弹出菜单 本代码是人民邮电出版社出版的《v c++程序开发范例宝典》的代码。测试通过。-Taskbar tray pop-up menu code is the people of the Posts and Telecommunications Publishing House published
Platform: | Size: 425984 | Author: 刘超 | Hits:

[Internet-Networklighttpd-1.5.0.r2294.modcache.v.1.6.1.tar

Description: 带mod_cache的lighttpd服务器,性能有很大提高,可以让一个1G带宽跑满不是神话 使用是直接解压后./configure -C就可以生成mod_cache模块的lighttpd.-The lighttpd server with mod_cache, performance has greatly improved, allowing one run over 1G bandwidth is not a myth after use is a direct unzip./Configure-C can be generated on the mod_cache module lighttpd.
Platform: | Size: 907264 | Author: 林王 | Hits:

[GDI-BitmapV-C-graphicalinterface

Description: V C++图形用户界面,很详细的window图形界面设计文档-V C graphicalinterface
Platform: | Size: 17765376 | Author: xiaofeng | Hits:

[USB developV-USB

Description: 基于C#的USB上位机程序,调用libusbdotnet类实现的编程。比较简单明了-C#-based USB host computer program, called libusbdotnet class implements the program. Relatively simple
Platform: | Size: 280576 | Author: 吕凯悦 | Hits:

[OtherPIC单片机滚动码解码C程序

Description: //PIC单片机滚动码解码C程序 // Interrupt based receive routine 基于中断的接收例程 // Compiled using HiTech PIC C compiler v.7.93 编译使用该PIC的C编译器v.7.93 #define CLOCK 4 // MHz #define TE 400 // us #define OVERSAMPLING 3 // #define PERIOD //TE/OVERSAMPLING*4/CLOCK #define NBIT 65 // number of bit to receive -1(HCS300 66-1=65)(Interrupt based receive routine)
Platform: | Size: 5120 | Author: jihh04 | Hits:

[VC/MFCC coding of ABC

Description: c code of ABC. its v useful code. its v simple
Platform: | Size: 3072 | Author: zeeshan | Hits:
« 12 3 4 5 6 7 8 9 10 ... 49 »

CodeBus www.codebus.net